//
//  JSBaseViewController.m
//  JigSaw
//
//  Created by Slim Wang on 15/2/6.
//  Copyright (c) 2015年 LITB. All rights reserved.
//

#import "JSBaseViewController.h"
#import "UIColor+Help.h"
#import "UIImage+Help.h"
@interface JSBaseViewController ()
@property (nonatomic, strong) UIBarButtonItem * leftBarButtonItem;
@property (nonatomic, strong) UIBarButtonItem * rightBarButtonItem;

@end

@implementation JSBaseViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.navigationController.navigationBar.backgroundColor = [UIColor colorWithHex:0x171819 alpha:0.9];
//    UIImage *backImage = [UIImage imageNamed:@"bar_bg"];
//    [self.navigationController.navigationBar setBackgroundImage:backImage forBarMetrics:UIBarMetricsDefault];
    [self addLeftBackButton];
    UIFont * titleFont = [UIFont systemFontOfSize:18];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:titleFont}];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}

- (void)addLeftBackButton
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor clearColor];
    [button setBackgroundImage:[UIImage imageNamed:@"nav_bar_back"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"nav_bar_back_click"] forState:UIControlStateHighlighted];
    [button sizeToFit];
    [button addTarget:self action:@selector(onBackButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = leftBarButton;
    self.leftBarButtonItem = self.navigationItem.leftBarButtonItem;
}

- (void)onBackButtonPressed:(id)sender
{
    if (self.navigationController.viewControllers.count >= 2) {
        [self.navigationController popViewControllerAnimated:YES];
    }
    else
    {
        [self.navigationController dismissViewControllerAnimated:YES completion:NULL];
    }
}


- (void)addRightButton
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor clearColor];
    [button setBackgroundImage:[UIImage imageNamed:@"share_btn"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"share_btn_click"] forState:UIControlStateHighlighted];
    [button sizeToFit];
    [button addTarget:self action:@selector(onRightButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = leftBarButton;
    self.rightBarButtonItem = self.navigationItem.leftBarButtonItem;
}

- (void)onRightButtonPressed:(id)sender
{
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
